﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.Users;
using System.Data;

namespace Marbles.Web.Admin.Content
{
    public partial class UserMgr : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            User currentUser = User.GetCurrent();
            UserCollection items = new UserCollection(this.CurrentWeb.SiteId, !currentUser.Groups.Contains(4));
            cGrdUsers.DataSource = items;
            cGrdUsers.DataBind();
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            User user = null;

            switch (e.CommandName)
            {
                case "New":
                    LoadGroups(null);
                    cDlgUser.Title = "Create new User";
                    cDlgUser.Visible = true;
                    cBtnSave.CommandArgument = "";
                    cBtnSave.CommandName = "Create";
                    cLblUserId.Text = "";
                    cTxtUsername.Text = "";
                    cTxtFullname.Text = "";
                    cTxtPassword.Text = "";
                    cTxtEmail.Text = "";
                    cChkEnabled.Checked = true;
                    cTxtOpenID.Text = "";
                    cBtnDelete.CommandArgument = "";
                    cBtnDelete.Enabled = false;
                    break;
                case "Modify":
                    user = new User(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    LoadGroups(user);
                    cLblUserId.Text = user.UserId;
                    cTxtUsername.Text = user.Username;
                    cTxtFullname.Text = user.Fullname;
                    cTxtPassword.Text = user.Password;
                    cTxtEmail.Text = user.Email;
                    cChkEnabled.Checked = user.Enabled;
                    cTxtOpenID.Text = user.OpenID;
                    cDlgUser.Title = "Modify existing User";
                    cDlgUser.Visible = true;
                    cBtnSave.CommandArgument = e.CommandArgument.ToString();
                    cBtnSave.CommandName = "Save";
                    cBtnDelete.Enabled = true;
                    cBtnDelete.CommandArgument = e.CommandArgument.ToString();
                    break;
                case "Delete":
                    user = new User(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    user.Delete();
                    cDlgUser.Visible = false;
                    LoadData();
                    break;
                case "Create":
                    user = User.CreateNew(this.CurrentWeb.SiteId);
                    cLblUserId.Text = user.UserId;
                    user.Username = cTxtUsername.Text;
                    user.Fullname = cTxtFullname.Text;
                    user.Password = cTxtPassword.Text;
                    user.Email = cTxtEmail.Text;
                    user.Enabled = cChkEnabled.Checked;
                    user.OpenID = cTxtOpenID.Text;
                    UpdateGroups(user);
                    user.Insert();
                    cDlgUser.Visible = false;
                    LoadData();
                    break;
                case "Save":
                    user = new User(this.CurrentWeb.SiteId, e.CommandArgument.ToString());
                    user.Username = cTxtUsername.Text;
                    user.Fullname = cTxtFullname.Text;
                    if (!string.IsNullOrEmpty(cTxtPassword.Text)) { user.Password = cTxtPassword.Text; }
                    user.Email = cTxtEmail.Text;
                    user.Enabled = cChkEnabled.Checked;
                    user.OpenID = cTxtOpenID.Text;
                    UpdateGroups(user);
                    user.Update();                    
                    cDlgUser.Visible = false;
                    cBtnSave.CommandArgument = "";
                    LoadData();
                    break;
                case "Cancel":
                    cDlgUser.Visible = false;
                    break;
            }
        }

        private void UpdateGroups(User user)
        {
            user.Groups.Clear();
            foreach (ListItem item in cLstGroups.Items)
            {
                if (item.Selected)
                {
                    user.Groups.Add(Convert.ToInt32(item.Value));
                }
            }
        }


        private void LoadGroups(User user)
        {
            User currentUser = User.GetCurrent();
            cLstGroups.DataSource = UserCollection.GetGroups(!currentUser.Groups.Contains(4));
            cLstGroups.DataBind();
            if (user == null) { return; }

            cLstGroups.ClearSelection();

            foreach (ListItem item in cLstGroups.Items)
            {
                item.Selected = user.Groups.Contains(Convert.ToInt32(item.Value));
            }
        }
    }
}